package com.atguigu.consumer;

import com.atguigu.constant.MqConst;
import com.atguigu.entity.OrderInfo;
import com.atguigu.entity.PaymentInfo;
import com.atguigu.enums.OrderStatus;
import com.atguigu.enums.PaymentStatus;
import com.atguigu.enums.ProcessStatus;
import com.atguigu.service.PaymentInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;

@Component
public class PaymentConsumer {
    @Autowired
    private PaymentInfoService paymentInfoService;

    //2.关闭支付表单
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.CLOSE_PAYMENT_QUEUE, durable = "false"),
            exchange = @Exchange(value = MqConst.CLOSE_PAYMENT_EXCHANGE, durable = "false"),
            key = {MqConst.CLOSE_PAYMENT_ROUTE_KEY}))
    public void closePaymentStatus(String outTradeNo, Channel channel , Message message)throws Exception {
        if (!StringUtils.isEmpty(outTradeNo)) {
            //修改订单状态
            PaymentInfo paymentInfo = paymentInfoService.getPaymentInfo(outTradeNo);
            paymentInfo.setPaymentStatus(PaymentStatus.ClOSED.name());
            paymentInfoService.updateById(paymentInfo);
        }
    }
}
